from typing import *

class Solution:
    def sumSubarrayMins(self, arr: List[int]) -> int:
        q = []
        ans = 0
        for i in range(len(arr)):
            while len(q) > 0 and arr[i] <= arr[q[-1]]:
                j = q.pop()
                l = q[-1] if len(q) > 0 else -1
                ans += (j - l) * (i - j) * arr[j]
            q.append(i)
        while len(q) > 0:
            j = q.pop()
            l = q[-1] if len(q) > 0 else -1
            ans += (j - l) * (len(arr) - j) * arr[j]
        return ans % 1000000007